package com.spring.boot.demo.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/10/27 22:00
 * @Description
 */
@RestController
@RequestMapping("/api")
public class TestController {

    @GetMapping("/start")
    public String start() throws SchedulerException {
        // 1、创建调度器工厂
        SchedulerFactory factory = new StdSchedulerFactory();
        // 2、从工厂中获取具体的调度器
        Scheduler scheduler = factory.getScheduler();

        // 创建 JobDetail
        JobDetail jobDetail = JobBuilder.newJob(DemoJob.class)
                .withDescription("这是创建任务的描述信息")
                .withIdentity("这是任务名称", "这是任务分组名称")
                .build();

        // 创建调度器构建器
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?");

        // 创建触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withSchedule(cronScheduleBuilder)
                .withDescription("这是触发器的描述信息")
                .withIdentity("这是触发器名称", "这是触发器分组名称")
                .startNow()
                .build();

        // 绑定 Job 和 Trigger
        scheduler.scheduleJob(jobDetail, cronTrigger);
        // 启动
        scheduler.start();
        return "任务启动成功";
    }

}
